﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Camping_Project.Forms
{
    public partial class FormConsulter : Form
    {

        private int ligne, id;

        public FormConsulter()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            ligne = dataGridViewReserv.SelectedCells[0].RowIndex;
            id = (int)dataGridViewReserv["iDReservationDataGridViewTextBoxColumn", ligne].Value;

       
        }

        private void FormConsulter_Load(object sender, EventArgs e)
        {
            refreshDGVClient();
        }


        public void refreshDGVClient()
        {
            Camping_BDD bdd = new Camping_BDD();
            this.dataGridViewReserv.DataSource = from reservation in bdd.Reservation
                                                                            select new {
                                                                              reservation.IDReservation,
                                                                              reservation.DateDebut,
                                                                              reservation.DateFinApprox,
                                                                              reservation.DateFinDef,
                                                                              reservation.TypeEmp,
                                                                              reservation.NbAdultes,
                                                                              reservation.NbEnfants,
                                                                              reservation.NbAnimaux,
                                                                              reservation.NbVehicules,
                                                                              reservation.Electricite,
                                                                              reservation.PrixNuit,
                                                                              reservation.PrixAdulte,
                                                                              reservation.PrixEnfant,
                                                                              reservation.PrixAnimal,
                                                                              reservation.PrixElectricite,
                                                                              reservation.PrixTotal,
                                                                              reservation.PayementEffectue,
                                                                              reservation.IDClient,
                                                                              reservation.IDEmplacement,
                                                                              reservation.IDEmploye
                                                                            };

            dataGridViewReserv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (id != 0)
            {
                float pNuit = 0;
                Camping_BDD bdd = new Camping_BDD();
                var reserv = (from r in bdd.Reservation
                              where r.IDReservation == id
                              select r).Single();

                if (reserv.PrixTotal == 0)
                {

                    ObjectQuery<Tarif> tarif = bdd.Tarif;

                    //Selection des données du tarif dans la table Tarif
                    var query =
                            from tar in bdd.Tarif
                            select new
                            {
                                tar.PrixNuit,
                            };


                    foreach (var q in query)
                    {
                        pNuit = (float)q.PrixNuit;
                    }

                    TimeSpan differenceAD = DateTime.Parse(reserv.DateFinApprox.ToString()) - DateTime.Parse(reserv.DateDebut.ToString());
                    int diffAD = Convert.ToInt32(differenceAD.TotalDays);
                    reserv.DateFinDef = DateTime.Parse(departDef.Text);
                    TimeSpan diff = DateTime.Parse(reserv.DateFinDef.ToString()) - DateTime.Parse(reserv.DateDebut.ToString());
                    int diffInt = Convert.ToInt32(diff.TotalDays);
                    if (diffInt - diffAD > 3 || diffInt - diffAD < -3)
                    {
                        MessageBox.Show("La date de départ définitive choisie n'est pas dans les trois jours d'interval permis", "Operation échouée", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        reserv.PrixNuit = pNuit * (float)diffInt;
                        reserv.PrixTotal = reserv.PrixNuit + reserv.PrixAdulte + reserv.PrixEnfant + reserv.PrixAnimal + reserv.PrixVehicule + reserv.PrixElectricite;
                        bdd.SaveChanges();
                        refreshDGVClient();
                        MessageBox.Show("La date de départ définitive a été établie", "Operation effectuée", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                else
                {
                    MessageBox.Show("La date de départ définitive a déjà été établie!", "Operation non effectuée", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
                MessageBox.Show("Veuillez d'abord sélectionner un client!", "Operation non effectuée", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            
        }
    }
}
